<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<!--查询部门表单-->
<form class="layui-form" action="" lay-filter="departmentForm">
    <div class="layui-form-item" style="margin-top:10px">
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-block" style="width:200px">
                <input type="text" name="p_name" lay-verify="p_name" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">部门备注</label>
            <div class="layui-input-inline" style="width:200px">
                <input type="text" name="p_remark" lay-verify="p_remark" autocomplete="off" class="layui-input">
            </div>
        </div>
        <button type="button" id="formsubmit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>

        <span id="add" class="layui-btn" lay-submit="" lay-filter="pageSubmit">新增部门</span>
    </div>
</form>
<!--部门表单-->
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a id="del" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="../../layui/layui.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use(['table','form','layer'], function(){
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        table.render({
            id : 'departmentTable'
            ,elem: '#test'
            ,url:'/firstWeb/layuidepartment'
            ,title: '用户数据表'
            ,where: {}
            ,cols: [[
                {field:'p_id', title:'部门编号'}
                ,{field:'p_name', title:'部门名称'}
                ,{field:'p_remark', title:'部门备注'}
                ,{field:'p_is' ,title:'是否已注销'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            ,page: true // 默认一页十条
            ,limit:5
            ,limits: [1,5,10,20]
        });
        function tableReload() {
            var data = form.val('departmentForm');
            table.reload('departmentTable',{
                where : data
                ,page: {
                    curr: 1
                }
            })
        }

        $("#formsubmit").click(function(){
            tableReload();
        })
        $("#add").click(function(){

            layer.open({
                type: 2 //此处以iframe举例
                ,title: '新增页面'
                ,area: ['390px', '260px']
                ,shade: 0
                ,maxmin: true
                ,content: 'addPart.html'
                ,btn: ['提交', '关闭']
                ,yes: function(index){
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.p_name) {
                        layer.msg("部门名称不能为空");
                        return false;
                    }

                    //发送新增请求 get/post √
                    axios.post('/firstWeb/layuidepartment', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "0") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                ,btn2: function(index){//5
                    layer.close(index);
                }
            });
        })

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index) {
                    axios.post('/firstWeb/updatePart', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.status == "1") {
                            layer.close(index);
                            //刷新表格
                            tableReload();
                            layer.msg("注销成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                })
            } else if(obj.event === 'edit'){
                    layer.open({
                        type: 2 //此处以iframe举例
                        ,title: '新增页面'
                        ,area: ['390px', '340px']
                        ,shade: 0
                        ,maxmin: true
                        ,content: 'updatePart.html'
                        ,btn: ['提交', '关闭']
                        ,yes: function(index){
                            data = window['layui-layer-iframe' + index].getValue();
                            data.p_id = obj.data.p_id
                            if (!data.p_name) {
                                layer.msg("部门名称不能为空");
                                return false;
                            }

                            //发送新增请求 get/post √
                            axios.post('/firstWeb/update', data).then(function (response) {
                                //post请求执行成功的回调
                                if (response.data.status == "2") {
                                    layer.close(index);
                                    //刷新表格
                                    tableReload();
                                    layer.msg("修改成功");
                                } else {
                                    layer.msg(response.data.msg)
                                }
                            }).catch(function (error) {
                                console.log(error);
                            });
                        },
                        success:function(layero ,index){
                            <!--给弹出表单赋值-->
                            var div = layero.find('iframe').contents().find('#part');
                            div.find("#p_id").val(data.p_id);
                            div.find("#p_name").val(data.p_name);
                            div.find("#p_remark").val(data.p_remark);
                            div.find("#p_is").val(data.p_is);
                        }
                        ,btn2: function(index){//5
                            layer.close(index);
                        }
                    });
            }
        });
    });
</script>

</body>
</html>